package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 25169
 * @since 2023-01-24
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 会员登录
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败");
        }

        //判断账号
        UcenterMember ucenterMember = baseMapper.selectOne(new LambdaQueryWrapper<UcenterMember>().eq(UcenterMember::getMobile, mobile));
        if(ucenterMember == null) {
            throw new GuliException(20001,"登录失败");
        }
        //判断密码
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"登录失败");
        }
        //判断账户是否禁用
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }

        //登录成功后
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code))
        {
            throw new GuliException(20001,"注册失败");
        }

        //判断验证码
        String s = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(s)){
            throw new GuliException(20001,"注册失败");
        }

        //判断手机号是否重复
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<UcenterMember>().eq(UcenterMember::getMobile, mobile);
        int count = this.count(queryWrapper);
        if(count>0){
            throw new GuliException(20001,"注册失败");
        }

        //添加数据
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://tedu-1010.oss-cn-hangzhou.aliyuncs.com/2023/01/21/1b908a29c101477a8264441cba4c96eb.jpg");
        this.save(ucenterMember);
    }

    /**
     * 判断是否存在用户
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getByOpenid(String openid) {
        LambdaQueryWrapper<UcenterMember> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getOpenid,openid);
        UcenterMember one = this.getOne(queryWrapper);
        return one;
    }

    /**
     * 查询某一天有多少注册人数
     * @param day
     * @return
     */
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
